var app=getApp();
var that;
var c
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    that=this
    if(!await app.hasUserInfo()){
      wx.navigateTo({
        url: '/pages/login/login'
      })
    }
    const db = wx.cloud.database({
      env: "cloud2-4gncvam39e2323e9",
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    that = this
    this.setData({
      interval:setInterval(function () {
        //console.log("666")
        that.updateData()
      }, 1000) //循环时间 这里是1秒 
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    that.updateData()
  },

 tochatroom:function(options){
    console.log(options.currentTarget.dataset.all)
    var openid2 = options.currentTarget.dataset.all.openid2
    var avatarUrl2 = options.currentTarget.dataset.all.avatar
    var nickName2 = options.currentTarget.dataset.all.nickName
    console.log("in detail",openid2,avatarUrl2,nickName2)
    wx.navigateTo({
      url: '../room/room?openid2='+ openid2+'&avatarUrl2='+ avatarUrl2+'&nickName2='+nickName2
    })
  },

  updateData(){
   // wx.hideNavigationBarLoading(); //隐藏加载
    wx.cloud.callFunction({
      name:"getlastchat"
    }).then(res=>{
      c = res.result.data;
      that.setData({
        state:  c,
      })
      wx.stopPullDownRefresh();
    })
  },
  onUnload() {
    //页面销毁停止运行
    let that = this
    clearInterval(that.data.interval)
  },
  onHide:function(){ 
		let that = this;
		clearInterval(that.data.interval);
	},

  Search(){
    wx.navigateTo({
      url: '/pages/search/search'
    })
  }
})